package com.neudu.glsb.aspect;

import com.neudu.glsb.pojo.UserOperationLogs;
import com.neudu.glsb.service.UserOperationLogsService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


@Aspect
@Component
public class UserOperationLogAspect {

//    @Autowired
//    private UserOperationLogsService userOperationLogService;
//
//    // 定义切点，拦截所有以 Service 结尾的类中的所有方法
//    @Pointcut("execution(* com.neudu.glsb.service.*Service.*(..))")
//    public void operationPointcut() {
//    }
//
//    @After("operationPointcut()")
//    public void recordOperation(JoinPoint joinPoint) {
//        try {
//            // 检查安全上下文是否存在认证信息
//            if (SecurityContextHolder.getContext().getAuthentication() != null
//                && SecurityContextHolder.getContext().getAuthentication().getName() != null) {
//                Integer userId = Integer.parseInt(SecurityContextHolder.getContext().getAuthentication().getName());
//                UserOperationLogs log = new UserOperationLogs();
//                log.setUserId(userId);
//                log.setOperationTime(new Date());
//                log.setOperationType(joinPoint.getSignature().getName());
//                log.setOperationDetail("调用了 " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + " 方法");
//                // 确保 Service 正确注入，避免空指针异常
//                if (userOperationLogService != null) {
//                    userOperationLogService.save(log);
//                }
//            }
//        } catch (NumberFormatException e) {
//            // 处理转换异常
//            e.printStackTrace();
//        }
//    }
}